[C] Agenda - LDE
Publicado por Enzo de Brito Ferber (última atualização em 15/09/2010)
[ Hits: 6.655 ]
Homepage: http://www.maximasonorizacao.com.br
Agenda simples com funções para inserir, remover e procurar. Uso de listas duplamente encadeadas.
http://www.vivaolinux.com.br/artigo/Linguagem-C-Listas-Duplamente-Encadeadas/
// agenda.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MALLOC(a) (a *) malloc( sizeof( a ))
#define MAX 50
// descomentar abaixo em linux
#define fflush(stdin) __fpurge(stdin)
struct contato
{
char nome[ MAX ];
char telefone[ MAX ];
char celular[ MAX ];
char email[ MAX ];
char obs[ MAX ];
struct contato *next;
struct contato *prior;
};
// variaveis para começo e fim da lista
struct contato *head;
struct contato *last;
void inserirdados( struct contato *novo )
{
struct contato *current;
// primeiro elemento da lista
if( !head )
{
head = last = novo;
novo->next = NULL;
novo->prior = NULL;
return ;
}
current = head;
while( current )
{
// comparação....
if( strcmp( current->nome, novo->nome ) < 0 )
current = current->next;
else
{
// elemento no meio da lista
if( current->prior )
{
novo->next = current;
novo->prior = current->prior;
current->prior->next = novo;
current->prior = novo;
return ;
}
// novo primeiro elemento da lista
novo->next = current;
novo->prior = NULL;
head->prior = novo;
head = novo;
return ;
}
}
// ultimo elemento da lsta
novo->next = NULL;
novo->prior = last;
last->next = novo;
last = novo;
return ;
}
struct contato *pesquisar( char *nome )
{
struct contato *current;
current = head;
while( current )
{
if( strcmp( current->nome, nome ) == 0 ) return current;
current = current->next;
}
return NULL;
}
void removercontato( struct contato *del )
{
// unico item
if ( head == last )
{
free( head );
head = last = NULL;
return ;
}
// primeiro item
if( !del->prior )
{
head = del->next;
head->prior = NULL;
free( del );
return ;
}
// ultimo item
if( !del->next )
{
last = del->prior;
last->next = NULL;
free( del );
return ;
}
// item no meio
del->prior->next = del->next;
del->next->prior = del->prior;
free( del );
return ;
}
void interfaceinserir( void )
{
struct contato *novo = MALLOC( struct contato );
// cls - windows
// clear - linux
system( "clear" );
printf( "\tNome : " ); fflush( stdin ); scanf( "%[^\n]", novo->nome );
printf( "\tTelefone : " ); fflush( stdin ); scanf( "%[^\n]", novo->telefone );
printf( "\tCelular : " ); fflush( stdin ); scanf( "%[^\n]", novo->celular );
printf( "\tE-mail : " ); fflush( stdin ); scanf( "%[^\n]", novo->email );
printf( "\tObservacoes: " ); fflush( stdin ); scanf( "%[^\n]", novo->obs );
// inserir
inserirdados( novo );
return ;
}
void interfaceremover( void )
{
char remover[ MAX ];
struct contato *del;
// cls - windows
// clear - linux
system( "clear" );
printf( "\tNome : " ); fflush( stdin ); scanf( "%[^\n]", remover );
del = pesquisar( remover );
if( del ) removercontato( del );
}
void interfacepesquisar( void )
{
struct contato *show;
char nome[ MAX ];
// cls - windows
// clear - linux
system( "clear" );
printf( "\tNome : " ); fflush( stdin ); scanf( "%[^\n]", nome );
show = pesquisar( nome );
if ( !show ) return ;
// cls - windows
// clear - linux
system( "clear" );
puts( "\n" );
printf( "\tNome : %s\n", show->nome );
printf( "\tTelefone : %s\n", show->telefone );
printf( "\tCelular : %s\n", show->celular );
printf( "\tE-mail : %s\n", show->email );
printf( "\tObservacoes: %s\n", show->obs );
fflush( stdin ); getchar();
return ;
}
void destruirlista( void )
{
struct contato *current;
current = head;
if( !head ) return ;
while( current->next )
{
current = current->next;
free( current->prior );
}
free( last );
return ;
}
void sair( void )
{
destruirlista();
exit( EXIT_SUCCESS );
}
void listar( void )
{
struct contato *current;
// cls - windows
// clear - linux
system( "clear" );
current = head;
if( !head )
{
printf( "Lista vazia.\n");
fflush( stdin ); getchar();
}
while( current )
{
printf( "%s\n", current->nome );
current = current->next;
}
fflush( stdin ); getchar();
return ;
}
// matriz de ponteiros para função
// o uso deste recurso faz com que o uso de switch...case seja dispensável
void (* funcs[5])(void) = {interfaceinserir, interfaceremover, interfacepesquisar, listar, sair};
void interfaceprincipal( void )
{
int op;
while( 1 )
{
// cls - windows
// clear - linux
system( "clear" );
printf( "\tMENU\n\n" );
printf( "\t1. INSERIR\n" );
printf( "\t2. REMOVER\n" );
printf( "\t3. INFORMACOES\n" );
printf( "\t4. LISTAR\n" );
printf( "\t5. SAIR\n" );
printf( "\n\t$ " );
fflush( stdin ); scanf( "%d", &op );
if( op >= 1 && op <= 5 )
(*funcs[op - 1])();
}
return ;
}
int main ( void )
{
interfaceprincipal();
return ;
}
Retornar o montante de um capital aplicado a juros compostos
Programa para demonstrar operadores
Conversor decimal para binario para GNU/LINUX
Nenhum comentário foi encontrado.
Cirurgia para acelerar o openSUSE em HD externo via USB
Void Server como Domain Control
Modo Simples de Baixar e Usar o bash-completion
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
Atualizar Linux Mint 22.2 para 22.3 beta
Jogar games da Battle.net no Linux com Faugus Launcher
Como fazer a Instalação de aplicativos para acesso remoto ao Linux
Conky, alerta de temperatura alta (10)
Assisti Avatar 3: Fogo e Cinzas (3)
Duas Pasta Pessoal Aparecendo no Ubuntu 24.04.3 LTS (42)









